//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.haitao.appupdate_library_project.R;

/**
 * @author liaoyong
 * @desc 更新activity
 * @date 2016/4/11 13:57
 */
public class CheckUpdateAcitivty extends Activity implements View.OnClickListener {
    private static final String TAG = "CheckUpdateAcitivty";
    AppInfo mInfo;
    private Button mUpdateBtn;
    private ResultReceiver resultReceiver = null;
    private View mLineView;

    public CheckUpdateAcitivty() {
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.umeng_update_dialog);
        Bundle args = this.getIntent().getExtras();
        this.mInfo = (AppInfo)args.getParcelable("UpdateInfo");
        if(this.mInfo == null) {
            finish();
        } else {
            initData();
            resultReceiver = (ResultReceiver) this.getIntent().getParcelableExtra("ResultReceiver");
        }

    }

    public void initData() {
        TextView titleTv = (TextView) this.findViewById(R.id.ument_update_title);
        titleTv.setText(String.format("%s%s", this.mInfo.updateTitle, this.mInfo.versioName));
        TextView contentTv = (TextView) this.findViewById(R.id.umeng_update_content);
        contentTv.setText(this.mInfo.updateComment);
        mLineView = this.findViewById(R.id.item_line);
        mUpdateBtn = (Button) this.findViewById(R.id.umeng_update_id_ok);
        mUpdateBtn.setOnClickListener(this);
        Button igoreBtn = (Button) this.findViewById(R.id.umeng_update_id_cancel);
        igoreBtn.setOnClickListener(this);
        ViewGroup.MarginLayoutParams mp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);  //item的宽高

        if (this.mInfo.isForcedUpdate ) {
            igoreBtn.setVisibility(View.GONE);
            mLineView.setVisibility(View.GONE);
        } else {
            igoreBtn.setVisibility(View.VISIBLE);
        }
    }

    private void doDirectDownload() {
        try {
            UpdateManager.doUpdate(this, this.mInfo, true, true, new UpdateManager.DownloadListener() {
                @Override
                public void onDownloadStart(AppInfo var1) {

                }

                @Override
                public void onDownloadProgress(AppInfo var1, long process, long max) {
                    Log.d("test", "process: " + process*100/max);
                    mainHandler.sendEmptyMessage((int) (process*100/max));
                }

                @Override
                public void onDownloadCompleted(AppInfo var1, boolean var2, Bundle var3) {

                }
            });
        } catch (Exception var2) {
            if(UpdateManager.isDebug()) {
                Log.w("CheckUpdateAcitivty", "doDirectDownload fail ", var2);
            }
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mInfo.isForcedUpdate) {
            final Bundle resultData = new Bundle();
            if(mInfo != null) {
                resultData.putInt(UpdateManager.KEY_ERROR_CODE, UpdateManager.ERROR_OK);
                resultData.putParcelable("UpdateInfo", mInfo);
                resultData.putInt(UpdateManager.KEY_ERROR_EXT_CODE, UpdateManager.RESULT_ClickBACK);
            }
            resultReceiver.send(UpdateManager.RESULT_ClickBACK, resultData);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.umeng_update_id_ok) {
            this.doDirectDownload();
            if (mInfo.isForcedUpdate) {
                mUpdateBtn.setText("正下载...");
            } else {
                finish();
            }
        } else {
            final Bundle resultData = new Bundle();
            if(mInfo != null) {
                resultData.putInt(UpdateManager.KEY_ERROR_CODE, UpdateManager.ERROR_OK);
                resultData.putParcelable("UpdateInfo", mInfo);
                resultData.putInt(UpdateManager.KEY_ERROR_EXT_CODE, UpdateManager.RESULT_ClickCancel);
            }
            resultReceiver.send(UpdateManager.RESULT_ClickCancel, resultData);

            finish();
        }
    }

    private Handler mainHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mUpdateBtn.setText(String.format("正下载%d", message.what) + "%");
            return false;
        }
    });
}
